package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.Member;
import com.wanshi.service.IMemberService;
import com.wanshi.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员信息表 前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-16
 */
@CrossOrigin
@RestController
@RequestMapping("/member")
@Api(tags = "测试MembelController接口文档")
public class MemberController {

    @Autowired
    private IMemberService memberService;


    @ApiOperation(value = "会员带条件分页")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="ye", value="页数", paramType = "path",required = true),
                    @ApiImplicitParam(name="size", value="一页显示多少条", paramType = "path", required = true)
            }
    )
    @PostMapping("/findPageAndCountMem/{ye}/{size}")
    public Result findPageAndCountMem(@PathVariable int ye,@PathVariable int size,@RequestBody Member member){
        System.out.println(member+"瞎眼好");
        Page page = new Page(ye,size);
        QueryWrapper<Member> wrapper = new QueryWrapper();
        if(member!=null){
            if(!StringUtils.isEmpty(member.getName())){
                wrapper.like("name", member.getName());
            }
            if(!StringUtils.isEmpty(member.getCardNum())){
                wrapper.like("card_num", member.getCardNum());
            }
            if(!StringUtils.isEmpty(member.getPayType())){
                wrapper.eq("pay_type", member.getPayType());
            }
            if(member.getBirthday() != null){
                wrapper.ge("birthday", member.getBirthday());
            }
        }
        wrapper.orderByDesc("id");
        memberService.page(page,wrapper);
        return Result.ok(page);
    }


    @ApiOperation(value = "根据id查找会员")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @GetMapping("/{id}")
    public Result findByIdMem(@PathVariable String id) {
        return Result.ok(memberService.getById(id));
    }

    @ApiOperation(value = "添加会员")
    @PostMapping("addMember")
    public Result addMember(@RequestBody Member member) {
        boolean b = memberService.save(member);
        if(b){
            return Result.ok();
        }
       return Result.error("添加会员失败");
    }


    @ApiOperation(value = "根据id删除会员")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteMember(@PathVariable String id){
        boolean b = memberService.removeById(id);
        if(b){
            return Result.ok();
        }
        return Result.error("删除会员失败");
    }

    @ApiOperation(value = "添加或者修改会员")
    @PostMapping("/member")
    public Result UpdateandAddMember(@RequestBody Member member) {
        boolean b = memberService.saveOrUpdate(member);
        if(b){
            return Result.ok();
        }
        return Result.error("修改会员失败");
    }


}

